package org.openfidias.jdbase.model;

import java.io.Serializable;
import java.text.DecimalFormat;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.openfidias.jdbase.db.IRegister;
import org.openfidias.jdbase.db.Register;

/**
 *
 * @author atila
 */
public class Disciplina extends Register implements Serializable, Comparable, IRegister {
   
   private static final long serialVersionUID = 1L;
   private int codigo;
   private String nome;
   private String curso;

   /**
    * constants for limit character size
    */
   public static final int maxCodigo = 3;
   public static final int maxNome = 20;
   public static final int maxCurso = 20;

   public Disciplina() {
      this.codigo = 0;
      this.nome = "";
      this.curso = "";
      this.deleted = 0;
   }

   public Disciplina(int codigo) {
      this.codigo = codigo;
      this.nome = "";
      this.curso = "";
      this.deleted = 0;
   }

   public Disciplina(int codigo, String nome, String curso) {
      this.codigo = codigo;
      this.nome = nome;
      this.curso = curso;
      this.deleted = 0;
   }

   public Disciplina(int codigo, String nome, String curso, short deleted) {
      this.codigo = codigo;
      this.nome = nome;
      this.curso = curso;
      this.deleted = deleted;
   }

   public int getCodigo() {
      return codigo;
   }

   public void setCodigo(int codigo) {
      this.codigo = codigo;
   }

   public String getCurso() {
      return curso;
   }

   public void setCurso(String curso) {
      this.curso = curso;
   }

   public String getNome() {
      return nome;
   }

   public void setNome(String nome) {
      this.nome = nome;
   }

   public int compareTo(Object obj) {
      Disciplina disciplina = (Disciplina) obj;
      return new Integer(codigo).compareTo(disciplina.getCodigo());
   }

   public String toLine() {
      StringBuilder builder = new StringBuilder();
      builder.append(new DecimalFormat("0").format(this.deleted));
      builder.append(":");
      builder.append(new DecimalFormat("000").format(this.codigo));
      builder.append(":");
      builder.append(this.nome);
      builder.append(":");
      builder.append(this.curso);
      builder.append(":");
      String line = StringUtils.rightPad(builder.toString(), size());
      return line;
   }

   public int size() {
      return maxCodigo + maxCurso + maxNome + 5;// <<5>> from 4 ":" and 1 for "deleted"
   }

   @Override
   public boolean equals(Object obj) {
      if (!(obj instanceof Disciplina)) {
         return false;
      }
      Disciplina disciplina = (Disciplina) obj;
      return new Integer(codigo).equals(disciplina.codigo);
   }

   @Override
   public int hashCode() {
      return new HashCodeBuilder().append(this.codigo).toHashCode();
   }

   @Override
   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append("|");
      builder.append(new DecimalFormat("000").format(this.codigo));
      builder.append("   |");
      builder.append(StringUtils.rightPad(this.nome, maxNome));
      builder.append("|");
      builder.append(StringUtils.rightPad(this.curso, maxCurso));
      builder.append("|");
      return builder.toString();
   }

   public static String headerTable() {
      StringBuilder builder = new StringBuilder();
      builder.append(rowSeparator());
      builder.append("\n|");
      builder.append(StringUtils.rightPad("Código", maxCodigo + 3));
      builder.append("|");
      builder.append(StringUtils.rightPad("Nome", maxNome));
      builder.append("|");
      builder.append(StringUtils.rightPad("Curso", maxCurso));
      builder.append("|\n");
      builder.append(rowSeparator());
      return builder.toString();
   }

   public static String rowSeparator() {
      StringBuilder builder = new StringBuilder();
      builder.append("+");
      builder.append(StringUtils.repeat("-", maxCodigo + 3));
      builder.append("+");
      builder.append(StringUtils.repeat("-", maxNome));
      builder.append("+");
      builder.append(StringUtils.repeat("-", maxCurso));
      builder.append("+");
      return builder.toString();
   }

   /**
    * Alias for rowSeparator.
    * @return
    */
   public static String footerTable() {
      return rowSeparator();
   }

   /**
    * TODO: mudar retorno do metodo para void!
    * @param line
    * @return
    */
   public IRegister fromLine(String line) {
      String[] array = line.split(":");
      this.deleted = Short.parseShort(array[0]);
      this.codigo = Integer.parseInt(array[1]);
      this.nome = array[2];
      this.curso = array[3];
      return this;
   }

   public boolean equals(String line) {
      String[] array = line.split(":");
      return isNotDeleted() && (
              new Integer(codigo).equals(new Integer(array[1]))
      );
   }
}
